﻿using System.Drawing;

namespace Charting
{
    public class DataPoint
    {
        public static readonly DataPoint Empty = new DataPoint(float.NaN, float.NaN);

        private Series mSeries;
        private float mX;
        private float mY;
        private bool mIsValueShownAsLabel;
        private Color mLineColor;
        private Color mPointColor;
        private Color mLabelForeColor;
        private Color mLabelBackColor;
        private int mPointSize;

        public DataPoint():this(0, 0)
        {
        }

        public DataPoint(float x, float y)
        {
            mX = x;
            mY = y;
            mSeries = null;
            mIsValueShownAsLabel = false;
            mLineColor = Color.Black;
            mPointColor = Color.Black;
            mLabelForeColor = Color.White;
            mLabelBackColor = Color.Black;
            mPointSize = 0;
        }

        public bool IsEmpty
        {
            get { return float.IsNaN(X) && float.IsNaN(Y); }
        }

        public float X
        {
            get { return mX; }
            set
            {
                if (mX != value)
                {
                    mX = value;
                    UpdateSeries(false);
                }
            }
        }

        public float Y
        {
            get { return mY; }
            set
            {
                if (mY != value)
                {
                    mY = value;
                    UpdateSeries(false);
                }
            }
        }

        public bool IsValueShownAsLabel
        {
            get { return mIsValueShownAsLabel; }
            set
            {
                if (mIsValueShownAsLabel != value)
                {
                    mIsValueShownAsLabel = value;
                    UpdateSeries(true);
                }  
            }
        }

        public Color LineColor
        {
            get { return mLineColor; }
            set
            {
                if (mLineColor != value)
                {
                    mLineColor = value;
                    UpdateSeries(true);
                }  
            }
        }

        public Color PointColor
        {
            get { return mPointColor; }
            set
            {
                if (mPointColor != value)
                {
                    mPointColor = value;
                    UpdateSeries(true);
                } 
            }
        }

        public Color LabelForeColor
        {
            get { return mLabelForeColor; }
            set
            {
                if (mLabelForeColor != value)
                {
                    mLabelForeColor = value;
                    UpdateSeries(true);
                }   
            }
        }

        public Color LabelBackColor
        {
            get { return mLabelBackColor; }
            set
            {
                if (mLabelBackColor != value)
                {
                    mLabelBackColor = value;
                    UpdateSeries(true);
                }
            }
        }

        public int PointSize
        {
            get { return mPointSize; }
            set
            {
                if (mPointSize != value)
                {
                    mPointSize = value;
                    UpdateSeries(true);
                }
            }
        }

        internal Series Series
        {
            get { return mSeries; }
            set
            {
                mSeries = value;
                if (mSeries != null)
                {
                    mIsValueShownAsLabel = mSeries.IsValueShownAsLabel;
                    mLineColor = mSeries.LineColor;
                    mPointColor = mSeries.PointColor;
                    mLabelForeColor = mSeries.LabelForeColor;
                    mLabelBackColor = mSeries.LabelBackColor;
                    mPointSize = mSeries.PointSize;
                }
            }
        }

        private void UpdateSeries(bool updateContentOnly)
        {
            if (mSeries == null)
                return;
            if (!updateContentOnly)
                mSeries.UpdatePoints();
            mSeries.ChartUpdateContent();
        }
    }
}
